package com.partvoyage.server.service.picassa;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import com.google.gdata.client.Query;
import com.google.gdata.client.authn.oauth.OAuthException;
import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.Kind.AdaptorException;
import com.google.gdata.data.geo.Point;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.AlbumFeed;
import com.google.gdata.data.photos.GphotoEntry;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import com.partvoyage.client.service.picassa.PicassaService;
import com.partvoyage.server.servlet.GAuthentificatedRemoteServiceServlet;
import com.partvoyage.server.utils.Configuration;
import com.partvoyage.shared.ClientConstants.AppScope;
import com.partvoyage.shared.PhotoBean;

/**
 * http://code.google.com/intl/fr-FR/apis/picasaweb/docs/2.0/
 * developers_guide_java.html#Auth
 * 
 * @author ludovic
 * 
 */
public class PicassaServiceImpl extends GAuthentificatedRemoteServiceServlet
		implements PicassaService {
	private final static Logger log = Logger.getLogger(PicassaServiceImpl.class
			.getName());

	private final static URL LIST_ALL_PICASSA_URL;
	// private final static String LIST_ALL_CALENDAR_EVENTS =
	// "https://www.google.com/calendar/feeds/{0}/private/full";

	static {
		try {
			LIST_ALL_PICASSA_URL = new URL(
					"https://picasaweb.google.com/data/feed/api/user/username?kind=album");
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	protected PicasawebService getAuthentificatedService()
			throws OAuthException {
		PicasawebService service = new PicasawebService(
				Configuration.getApplicationName());
		authentificate(service, AppScope.PICASSA);
		return service;
	}

	private static final int PHOTOS_PAR_PAGE = 10;

	// ESSAI 1
	/**
	 * http://gdata-java-client.googlecode.com/svn/trunk/java/sample/photos/
	 * PicasawebClient.java
	 */
	public List<PhotoBean> listPhoto(String what) {
		List<PhotoBean> photos = new ArrayList<PhotoBean>();
		PicasawebService myService = new PicasawebService(
				Configuration.getApplicationName());
		PicasaWebClient picasaWebClient = new PicasaWebClient(myService,
				"ludobou@gmail.com", "");
		try {
			List<AlbumEntry> to = picasaWebClient.getAlbums();
			for (AlbumEntry albumEntry : to) {
				System.out.println(albumEntry.getTitle());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<PhotoEntry> too = null;
		try {
			too = picasaWebClient.getPhotos("paris", 100);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (PhotoEntry albumEntry : too) {
			System.out.println(albumEntry.getTitle());
		}

		return photos;
	}
	// ESSAI 2
	// public List<Photo> listPhoto() {
	// List<Photo> photos = new ArrayList<Photo>();
	//
	// try {
	//
	// // création d'une fabrique de documents
	// DocumentBuilderFactory fabrique = DocumentBuilderFactory
	// .newInstance();
	//
	// // création d'un constructeur de documents
	// DocumentBuilder constructeur = fabrique.newDocumentBuilder();
	// int index = 1 + 10 * PHOTOS_PAR_PAGE;
	// URL fluxRss = new URL(
	// "http://picasaweb.google.com/data/feed/api/all?kind=photo&q="
	// + "paris" + "&start-index=" + index
	// + "&max-results=" + 10);
	// Document d = constructeur.parse(fluxRss.openConnection()
	// .getInputStream());
	//
	// NodeList entrees = d.getElementsByTagName("entry");
	//
	// for (int i = 0; i < entrees.getLength(); i++) {
	// Photo pt = new Photo(entrees.item(i).getChildNodes().item(9)
	// .getAttributes().getNamedItem("href").getNodeValue(),
	// entrees.item(i).getChildNodes().item(6).getAttributes()
	// .getNamedItem("src").getNodeValue(), entrees
	// .item(i).getChildNodes().item(4)
	// .getFirstChild().getNodeValue());
	// photos.add(pt);
	// }
	// } catch (MalformedURLException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// } catch (SAXException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// } catch (IOException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// } catch (ParserConfigurationException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return photos;
	// }

	//
	// @Override
	// public void addEvent(CalendarEvent event) {
	// // TODO Auto-generated method stub
	//
	// }
	//
	// @Override
	// public void modifyEvent(CalendarEvent event) {
	// // TODO Auto-generated method stub
	//
	// }
	//
	// @Override
	// public void deleteEvent(CalendarEvent event) {
	// // TODO Auto-generated method stub
	//
	// }
	//

	//
	// private Calendar toSCMCalendar(CalendarEntry calendarEntry) {
	// Calendar calendar = new Calendar();
	//
	// calendar.setId(calendarEntry.getId());
	// calendar.setTitle(calendarEntry.getTitle() != null ? calendarEntry
	// .getTitle().getPlainText() : "");
	// calendar.setSummary(calendarEntry.getSummary() != null ? calendarEntry
	// .getSummary().getPlainText() : "");
	// calendar.setHidden(calendarEntry.getHidden().getValue()
	// .equals(HiddenProperty.TRUE.getValue()));
	// calendar.setTimeZone(calendarEntry.getTimeZone().getValue());
	//
	// return calendar;
	// }
	//
	// private CalendarEvent toSCMCalendarEvent(CalendarEntry cal,
	// CalendarEventEntry eventEntry) {
	// CalendarEvent calendarEvent = new CalendarEvent();
	//
	// calendarEvent.setCalendarId(cal.getId());
	// calendarEvent.setId(eventEntry.getId());
	// calendarEvent.setTitle(eventEntry.getTitle() != null ? eventEntry
	// .getTitle().getPlainText() : "");
	// calendarEvent.setContent(eventEntry.getPlainTextContent());
	//
	// When time = eventEntry.getTimes().isEmpty() ? null : eventEntry
	// .getTimes().get(0);
	// calendarEvent.setStartTime(time != null ? time.getStartTime()
	// .toString() : "");
	// calendarEvent.setEndTime(time != null ? time.getEndTime().toString()
	// : "");
	//
	// return calendarEvent;
	// }
	//
	// private List<CalendarEvent> toSCMCalendarEvents(CalendarEntry cal,
	// List<CalendarEventEntry> eventEntries) {
	// List<CalendarEvent> calendarEvents = new LinkedList<CalendarEvent>();
	//
	// for (CalendarEventEntry eventEntry : eventEntries) {
	// CalendarEvent calendarEvent = toSCMCalendarEvent(cal, eventEntry);
	// calendarEvents.add(calendarEvent);
	// }
	//
	// return calendarEvents;
	// }
	//
	// private List<CalendarEventEntry> listEventFeedsForCalendarEntry(
	// CalendarEntry calendarEntry, Date from, Date to) {
	// log.info("listEventFeedsForCalendarEntry():: start");
	//
	// List<CalendarEventEntry> eventEntries = new
	// LinkedList<CalendarEventEntry>();
	//
	// try {
	// String query = MessageFormat.format(LIST_ALL_CALENDAR_EVENTS,
	// CalendarUtils.getCalendarId(calendarEntry));
	// CalendarQuery calendarQuery = new CalendarQuery(new URL(query));
	//
	// calendarQuery.setMinimumStartTime(new DateTime(from));
	// calendarQuery.setMaximumStartTime(new DateTime(to));
	//
	// log.info("listEventFeedsForCalendarEntry():: query="
	// + calendarQuery.getFeedUrl());
	//
	// CalendarEventFeed results = getAuthentificatedCalendarService()
	// .getFeed(calendarQuery, CalendarEventFeed.class);
	// log.info("listEventFeedsForCalendarEntry():: calId="
	// + calendarEntry.getId() + ", events="
	// + results.getEntries().size());
	//
	// for (CalendarEventEntry calendarEvent : results.getEntries()) {
	// eventEntries.add(calendarEvent);
	// }
	// } catch (Exception ex) {
	// log.log(Level.WARNING, "listEventFeedsForCalendarEntry():: ", ex);
	// }
	//
	// log.info("listEventFeedsForCalendarEntry():: exit");
	//
	// return eventEntries;
	// }
}
